<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      layout:decorate="~{layout/default}">
<head>
    <meta charset="UTF-8">
    <title>帝一流</title>
    <link th:href="@{/static/plugin/bootstrap-table/bootstrap-table.css}" type="text/css" rel="stylesheet">
    <link th:href="@{/static/plugin/tagsinput/jquery.tagsinput.css}" type="text/css" rel="stylesheet">
</head>
<body>
<div layout:fragment="cm-flex" class="cm-flex">
    <div class="cm-breadcrumb-container">
        <ol class="breadcrumb">
            <li><a href="#">运维管理</a></li>
            <li class="active">平台部署</li>
        </ol>
    </div>
    <form id="cm-search" action="index.html" method="get">
        <input type="search" name="q" autocomplete="off" placeholder="Search...">
    </form>
</div>
<div layout:fragment="content" class="container-fluid">
    <div class="panel panel-default">
        <div class="panel-body auto-height">
            <div class="search-block" id="search-block">
                <form action="#" class="form-group form-horizontal">
                    <div class="row">
                        <div class="col-sm-4">
                            <input type="text" class="form-control" placeholder="地址/平台" id="search"
                                   data-toggle="completer" data-suggest="true">
                        </div>
                        <div class="col-sm-2 col-sm-offset-6 col-md-1 col-md-offset-7">
                            <button type="button" class="btn btn-primary btn-block" id="doSearch">查询</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="table-panel">
                <div class="btn-group" role="group" id="toolbar">
                    <button shiro:hasPermission="deploy:add" type="button" class="btn btn-default" id="addDeploy"><i class="fa fa-fw fa-plus"></i></button>
                    <button shiro:hasPermission="deploy:edit"  type="button" class="btn btn-default" id="editDeploy"><i class="fa fa-fw fa-pencil"></i></button>
                    <button shiro:hasPermission="deploy:delete"  type="button" class="btn btn-default" id="delDeploy"><i class="fa fa-fw fa-trash-o"></i></button>
                </div>
                <table id="deployTable"></table>
            </div>
        </div>
    </div>
</div>
<div layout:fragment="myJs">
    <div id="deployModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h5 class="modal-title" id="deployModalTitle">
                        添加平台
                    </h5>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="deployForm">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group">
                            <label for="os" class="col-sm-2 control-label">系统</label>
                            <div class="col-sm-10">
                                <select name="os" id="os" class="form-control">
                                    <option value="Windows">Windows</option>
                                    <option value="Linux">Linux</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="address" class="col-sm-2 control-label">地址</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="address" name="address" placeholder="地址">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="platform" class="col-sm-2 control-label">平台</label>
                            <div class="col-sm-10">
                                <div class="modal-tag">
                                    <input type="text" class="tags" id="platform" name="platform" placeholder="编辑">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="tag" class="col-sm-2 control-label">标签</label>
                            <div class="col-sm-10">
                                <div class="modal-tag">
                                    <input type="text" class="tags" id="tag" name="tag" placeholder="编辑">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label">用户名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="username" name="username" placeholder="用户名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="password" class="col-sm-2 control-label">密码</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="password" name="password" placeholder="密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="comment" class="col-sm-2 control-label">描述</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" name="comment" id="comment" rows="3"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="saveDeploy">保存</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <script th:src="@{/static/plugin/bootstrap-table/bootstrap-table.js}"></script>
    <script th:src="@{/static/plugin/tagsinput/jquery.tagsinput.js}"></script>
    <script th:inline="javascript">
        $(function () {
            var autoHeight = $("div.auto-height").height();
            var rowHeight = $('#search-block').height();
            var tableHeight = autoHeight - rowHeight;

            var $table = $('#deployTable');
            $table.bootstrapTable({
                // 条纹
                striped: true,
                height: tableHeight,
                toolbar: '#toolbar',
                pagination: true,
                paginationLoop: false,
                sidePagination: 'server',
                url: /*[[@{/opt/deployList}]]*/,
                method: 'post',
                contentType: "application/x-www-form-urlencoded",
                dataField: "data",
                queryParamsType: '',
                queryParams: function queryParams(params) {
                    var param = {
                        pageNo: params.pageNumber,
                        pageSize: params.pageSize,
                        search: $('#search').val()
                    };
                    return param;
                },
                formatLoadingMessage: function () {
                    return "正在加载数据中，请稍候……"
                },
                formatShowingRows: function (a, b, c) {
                    return "第 " + a + " / " + b + " 条，共 " + c + " 条 ";
                },
                formatRecordsPerPage: function (a) {
                    return "每页 " + a;
                },
                formatNoMatches: function () {
                    return '没有找到匹配的数据';
                },
                columns: [
                    {
                        field: '',
                        checkbox: true
                    },
                    {
                        field: 'os',
                        title: '系统'
                    },
                    {
                        field: 'address',
                        title: '地址',
                        align: 'center'
                    },
                    {
                        field: 'platform',
                        title: '平台',
                        formatter: function (value, row, index) {
                            var str = '';
                            if (value){
                                var array = value.split(',');
                                for (var i = 0; i < array.length; i++){
                                    str += '<span class="btn-info" style="display: inline-block">' + array[i] +' </span>&nbsp;&nbsp;';
                                }
                            }

                            return str;
                        }
                    },
                    {
                        field: 'tag',
                        title: '标签',
                        formatter: function (value, row, index) {
                            var str = '';
                            if (value){
                                var array = value.split(',');
                                for (var i = 0; i < array.length; i++){
                                    str += '<span class="btn-turquoise" style="display: inline-block">' + array[i] +' </span>&nbsp;&nbsp;';
                                }
                            }

                            return str;
                        }
                    },
                    {
                        field: 'username',
                        title: '用户名'
                    },
                    {
                        field: 'password',
                        title: '密码'
                    },
                    {
                        field: 'comment',
                        title: '备注'
                    }
                ]
            });

            $('#doSearch').on('click', function () {
                $table.bootstrapTable("refresh");
            });

            $('#platform').tagsInput({defaultText: '编辑', width: 'auto', height: 60});
            $('#tag').tagsInput({defaultText: '编辑', width: 'auto', height: 90});

            $('#addDeploy').on('click', function () {
                $('#deployForm')[0].reset();
                $('#platform').importTags('');
                $('#tag').importTags('');

                $('#deployModalTitle').html('添加平台');
                $('#id').val('');
                $('#deployModal').modal('show');
            });

            $('#saveDeploy').on('click', function () {
                var param = $('#deployForm').serialize();
                $.ajax({
                    type: $('#id').val() == '' ? 'post' : 'put',
                    url: /*[[@{/opt/deploy}]]*/,
                    data: param,
                    dataType: "json",
                    success: function (result) {
                        if (result == 1) {
                            $('#deployModal').modal('hide');
                            $('#deployTable').bootstrapTable('refresh');

                            alertTip('success', '保存成功');
                        } else {
                            alertTip('error', '保存失败');
                        }
                    }
                });
            });

            $('#editDeploy').on('click', function () {
                var deploys = $table.bootstrapTable('getSelections');
                if (deploys.length != 1) {
                    swal("请选择一条信息进行修改!");

                    return;
                }
                $('#deployForm')[0].reset();

                var deploy = deploys[0];
                $('#id').val(deploy['id']);
                $('#address').val(deploy['address']);
                $('#username').val(deploy['username']);
                $('#password').val(deploy['password']);
                $('#comment').val(deploy['comment']);

                $('#platform').importTags(deploy['platform']);
                $('#tag').importTags(deploy['tag']);

                // 下拉选中
                var options = document.getElementById('os').options;
                for (var i = 0; i < options.length; i++) {
                    var o = options[i];
                    if (o.value == deploy['os']) {
                        o.selected = true;
                    }
                }

                $('#deployModalTitle').html('修改平台');
                $('#deployModal').modal('show');
            });

            $('#delDeploy').on('click', function () {
                var deploys = $table.bootstrapTable('getSelections');
                if (deploys.length < 1) {
                    swal("请至少选择一条信息进行删除!");

                    return;
                }
                // 提示
                swal({
                        title: "确定删除选中平台?",
                        text: "删除后将无法恢复",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#e67e22",
                        confirmButtonText: "删除",
                        closeOnConfirm: true
                    },
                    function () {
                        var idArr = [];
                        for (var i = 0; i < deploys.length; i++) {
                            idArr[i] = deploys[i]['id'];
                        }
                        $.ajax({
                            type: 'delete',
                            url: /*[[@{/opt/deploy}]]*/,
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(idArr),
                            dataType: "json",
                            success: function (result) {
                                if (result == 1) {
                                    $('#deployModal').modal('hide');
                                    $('#deployTable').bootstrapTable('refresh');

                                    alertTip('success', '删除成功');
                                } else {
                                    alertTip('error', '删除失败');
                                }
                            }
                        });
                    });
            });
        });
    </script>
</div>
</body>
</html>